package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.DTO.SetmealDto;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ICategoryService;
import com.itheima.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    ISetmealService setmealService;

    @Autowired
    ICategoryService categoryService;

    //新增套餐
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> addMeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmealWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R<IPage> pageSetmeal(Integer page,Integer pageSize,String name){

        IPage<Setmeal> Ipage = new Page<>(page,pageSize);
        IPage<SetmealDto> Ipage2 = new Page<>(page,pageSize);


        //进行模糊查询
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null,Setmeal::getName,name);

        //数据继续查询
        setmealService.page(Ipage,wrapper);

        //由于要返回的是SetmealDto对象，进行复制,忽视里面的数据
        BeanUtils.copyProperties(Ipage,Ipage2,"records");
        List<Setmeal> records = Ipage.getRecords();


        List<Setmeal> setmealList = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            //查看是否套餐分类
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        Ipage.setRecords(setmealList);


        return R.success(Ipage);
    }

    //删除套餐
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids){

        setmealService.deleteSetmealWithDish(ids);

        return R.success("删除成功");
    }

    //起售、停售
    @PostMapping("/status/{id}")
    private R<String> changeStatus(@PathVariable Integer id,Long[] ids){
        for (Long mealId : ids) {
            Setmeal setmeal = setmealService.getById(mealId);
            setmeal.setStatus(id);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    //回显数据
    @GetMapping("/{id}")
    private R<SetmealDto> selectSetmealById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.selectDishWithSetmeal(id);
        return R.success(setmealDto);
    }

    //修改套餐
    @PutMapping()
    public R<String> updateSetmealById(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmealWithDish(setmealDto);
        return R.success("修改成功");
    }

    //根据套餐分类id查询该套餐分类下的套餐
    @Cacheable(value = "setmealCache",key = "'setmeal_'+#setmeal.categoryId")
    @GetMapping("/list")
    public R<List<Setmeal>> selectSetmealById(Setmeal setmeal){
        //1.条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus,setmeal.getStatus());

        //2，查询套餐
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }

}
